/*
 * IndividualView.java
 *
 * Created on 13. Juni 2008, 14:38
 */

package twi.query.template2.view;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JComponent;
import javax.swing.JFrame;

import com.hp.hpl.jena.ontology.Individual;

import prefuse.controls.ControlAdapter;
import prefuse.data.Tree;
import prefuse.util.GraphLib;
import prefuse.visual.VisualItem;
import twi.result.graph.PrefuseGraphFactory;
import twi.view.manipulation.TreeView;
import twi_project.TWI;

/**
 *
 * @author  Soeren
 */
public class IndividualView extends javax.swing.JPanel {

	private ClassChooseListener ccl;
	/** Creates new form IndividualView 
     * @param parent 
     * @param s */
    public IndividualView(ArrayList<String> s, ClassChooseListener parent) {
        initComponents();
        this.ccl = parent;
        Collections.sort(s);
        for (int i = 0; i < s.size(); i++)
        	this.jComboBox1.addItem(s.get(i));
        this.jCheckBox_vis.setSelected(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox_vis = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(twi_project.TWI_ProjectApp.class).getContext().getResourceMap(IndividualView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        jCheckBox_vis.setText(resourceMap.getString("jCheckBox_vis.text")); // NOI18N
        jCheckBox_vis.setName("jCheckBox_vis"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setToolTipText(resourceMap.getString("jButton1.toolTipText")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, 0, 217, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox_vis)
                .addGap(2, 2, 2)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox1)
                    .addComponent(jCheckBox_vis)
                    .addComponent(jButton1)))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
	if (ccl != null)
		ccl.chooseClass((String) this.jComboBox1.getSelectedItem());
}//GEN-LAST:event_jComboBox1ItemStateChanged

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
	Tree t = PrefuseGraphFactory.createGraph(TWI.getInstance().getOntologieModel());
	final JFrame jf = new JFrame();
	jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	jf.setLayout(new BorderLayout());
	ControlAdapter adapter = new ControlAdapter(){
		long last = 0;
    	private String lastN = null;
    	@Override
    	public void itemClicked(VisualItem item, MouseEvent e) {
    		String itemName = item.getString(GraphLib.LABEL);
    		long curr = System.currentTimeMillis();
    		if (curr - last < 200 && lastN != null && lastN.equals(itemName)){
    			Individual indi = TWI.getInstance().getOntologieModel().getIndividual(itemName);
    			if (indi == null)
    				return ;
    			System.out.println(indi);
    			last = 0;
    			for (int i = 0; i < jComboBox1.getItemCount(); i++)
    				if (jComboBox1.getItemAt(i).equals(indi.getLocalName())){
    					jComboBox1.setSelectedIndex(i);
    					jf.dispose();
    					break;
    				}
    		}
    		last = System.currentTimeMillis();
    		this.lastN = itemName;
    	}
	};
	JComponent comp = TreeView.demo(t, GraphLib.LABEL, adapter, 300, 300);
	jf.add(comp, BorderLayout.CENTER);
	jf.pack();
	jf.setLocation(300, 300);
	jf.setVisible(true);
	
}//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox_vis;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
	public boolean showVariable() {
		return this.jCheckBox_vis.isSelected();
	}

}
